package com.jsyh.maidu.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.MainActivity;
import com.jsyh.maidu.activity.NearbyShopActivity;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.model.Menu;
import com.squareup.picasso.Picasso;

import java.util.List;


/**
 * Created by Administrator on 2017/6/8.
 * 专题列表适配器
 */

public class GvSubjectAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<Menu> datas;
    MainActivity activity;

    public GvSubjectAdapter(Context context, List<Menu> datas, MainActivity activity){
        this.context=context;
        this.datas = datas;
        this.activity = activity;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view=inflater.inflate(R.layout.gv_item_subject,null);
            holder=new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }

        holder.bindData(context, ConfigValue.IMG_IP + datas.get(i).getMenu_img(), datas.get(i).getMenu_name());
        if (i == 0){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.setFragmentIndex(1);
                }
            });
        }
        if (i == 1){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.setFragmentIndex(2);
                }
            });
        }
        if (i == 2){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, NearbyShopActivity.class);
                    context.startActivity(intent);
                }
            });
        }

        return view;
    }

    class ViewHolder{
        private ImageView iv_icon;
        private TextView tv_info;

        public ViewHolder(View view){
            iv_icon= (ImageView) view.findViewById(R.id.iv_icon);
            tv_info= (TextView) view.findViewById(R.id.tv_info);
        }

        /**
         * 绑定数据
         */
        public void bindData(Context context, String src, String content) {
            Picasso.with(context).load(src).into(iv_icon);
            tv_info.setText(content);
        }
    }
}
